//
//  SXiangqingTwoViewController.m
//  News
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SXiangqingTwoViewController.h"

@interface SXiangqingTwoViewController ()<UIScrollViewDelegate ,UITextViewDelegate>

@property(nonatomic ,retain)NSString * setName;
@property(nonatomic ,retain)SNewsXiangQingOne * picObject;
@property(nonatomic ,retain)NSMutableArray * picObjectArr;
@property(nonatomic ,retain)NSMutableArray * picWenZiArr;
@property(nonatomic ,retain)NSMutableArray * PicWangzhiArr;
@property(nonatomic ,retain)NSMutableArray * WenZiArr;
@property(nonatomic ,retain)UITextView * textViewOne;
@property(nonatomic ,retain)UIScrollView * scrollView;
@property(nonatomic ,retain)UIImageView *imageViewOne;
@property(nonatomic ,assign)CGFloat F;
@property(nonatomic ,retain)NSArray * arr;
@property(nonatomic ,assign)NSInteger a;
@property(nonatomic ,retain)UILabel * ladelThree;
@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property(nonatomic ,retain)UIImageView *imageView;


@end

@implementation SXiangqingTwoViewController

-(void)dealloc
{
    
    [_PicStr release];
    [_setName release];
    [_picObject release];
    [_picObjectArr release];
    [_PicWangzhiArr release];
    [_picWenZiArr release];
    [_WenZiArr release];
    [_scrollView release];
    [_imageViewOne release];
    [_arr release];
    [_ladelThree release];
    [_activityIndicator release];
    [_imageView release];
    [super dealloc];
    
}

/**
 *  
 *TabBar隐藏
 */
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor blackColor];
    
    self.title = @"图片详情";


    //        //请求出现小菊花
        self.imageView  = [[UIImageView alloc] initWithFrame:self.view.bounds];
            _imageView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
            _imageView.alpha =1;
            [self.view addSubview:_imageView];
    
            _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
            [_activityIndicator setCenter:_imageView.center];
    _activityIndicator.backgroundColor= [UIColor grayColor];
            [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
            [_imageView addSubview:_activityIndicator];
    
            [_activityIndicator startAnimating];

    

    [self getData];
 
    
}


-(void)getData
{



    
    NSString * tempStr = [_PicStr substringFromIndex:6];

    NSString * tempStrOne = [tempStr stringByReplacingOccurrencesOfString:@"|" withString:@"/"];

    NSString * wangZhiStr = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/00%@.json", tempStrOne];
  
[SNetWorkHandle getDataWithURL:wangZhiStr completion:^(id result) {
    

    self.setName = [result objectForKey:@"setname"];
  self.arr  = [result objectForKey:@"photos"];
    self.picObjectArr = [[NSMutableArray alloc] init];
    self.picWenZiArr = [[NSMutableArray alloc] init];
    self.PicWangzhiArr = [[NSMutableArray alloc] init];
    self.WenZiArr = [[NSMutableArray alloc] init];
    
    for (NSDictionary * temp in _arr) {

        self.picObject = [[SNewsXiangQingOne alloc] initWithDic:temp];
        
   
        [_picObjectArr addObject:_picObject];
        [_picWenZiArr addObject:_picObject.note];
        [_PicWangzhiArr addObject:_picObject.imgurl];
        [_WenZiArr addObject:_picObject.note];
        
        
    }
    

    [self createView];
    [_imageView removeFromSuperview];
}];
    
}


-(void)createView
{
  
    CGFloat F1 = [[self class] heightWithText:_WenZiArr[0]];
    
     self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 3.4, self.view.frame.size.width, self.view.frame.size.height / 3.4)];
    
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
      _scrollView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_scrollView];

  self.textViewOne = [[UITextView alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height * 2 + 80, self.view.frame.size.width, F1 + 40)];
    
    _textViewOne.editable = NO;
    _textViewOne.textColor = [UIColor whiteColor];
    _textViewOne.text = _WenZiArr[0] ;
    _textViewOne.font = [UIFont systemFontOfSize:17];
//    _textViewOne.contentSize = CGSizeMake(0, 2000);
    _textViewOne.backgroundColor = [UIColor blackColor];
    _textViewOne.delegate =  self;
    [self.view addSubview:_textViewOne];
    

    for (NSInteger i = 0;  i < _PicWangzhiArr.count ; i++) {
  
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * i + self.view.frame.size.width, 0);

        UIImageView * im = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.scrollView.bounds.size.height)];
        
        [im sd_setImageWithURL:[NSURL URLWithString:_PicWangzhiArr[i]] placeholderImage:[UIImage imageNamed:@"``_3(NZ4U{L@4L7FN0J70DA"]];
        
        [self.scrollView addSubview:im];
        
    }


    UILabel * ladelOne = [[UILabel alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height * 2 + 50, self.view.frame.size.width - 55, 30)];
    ladelOne.text =  _setName;
    ladelOne.textColor = [UIColor whiteColor];
    ladelOne.backgroundColor = [UIColor blackColor];
    [self.view addSubview:ladelOne];
    [ladelOne release];
    

 self.ladelThree   = [[UILabel alloc] initWithFrame:CGRectMake(ladelOne.bounds.size.width  , _scrollView.frame.size.height * 2 + 50, 55, 30)];
    _ladelThree.text = [NSString stringWithFormat:@"1/%ld", (unsigned long)_arr.count];
    _ladelThree.textAlignment = NSTextAlignmentRight;
    _ladelThree.textColor = [UIColor whiteColor];
    _ladelThree.backgroundColor = [UIColor blackColor];
    [self .view addSubview:_ladelThree];
    [_ladelThree release];
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView) {
    
//   self.tabBarController.tabBar.hidden = YES;
        
    CGPoint  pointOne = _scrollView.contentOffset;
    
    self.a = (NSInteger)pointOne.x / self.view.frame.size.width;
        
       _textViewOne.text = _WenZiArr[_a];

        self.F = [[self class] heightWithText:_textViewOne.text];
        _textViewOne.frame = CGRectMake(0, _scrollView.frame.size.height * 2 + 80, self.view.frame.size.width, _F + 30);

           _ladelThree.text = [NSString stringWithFormat:@"%ld/%ld", _a + 1,(unsigned long)_arr.count];

   }

}


+(CGFloat)heightWithText:(NSString*)text
{
    //    设置一个字典保存文本属性
    
    NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    //    预设一个尺寸 文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(375, 1000);
    
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
    
    //    返回得到的高度
    return rect.size.height;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
